package com.aeoj.clientmanagement.domain.userscore.vo;

import com.aeoj.clientmanagement.domain.userscore.UserScore;
import com.aeoj.clientmanagement.domain.userscore.dto.UserScoreDTO;
import com.aeoj.clientmanagement.enums.userscore.RankEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 用户积分表
 * @TableName aeoj_user_score
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UserScoreVO {
    /**
     * 主键ID
     */
    private Long id;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 用户名字
     */
    private String username;

    /**
     * 用户头像
     */
    private String avatar;

    /**
     * 宣言
     */
    private String description;

    /**
     * 积分
     */
    private Long score;

    /**
     * 段位
     */
    private String rankName;
    /**
     * 排名
     */
    private String rank;

    /**
     * 对战总场数
     */
    private Integer totalMatches;

    /**
     * 赢场数
     */
    private Integer winMatches;

    /**
     * 输场数
     */
    private Integer loseMatches;

    /**
     * 将 UserScore 实体转换为 UserScoreVO
     */
    public static UserScoreVO toUserScoreVO(UserScore userScore) {
        if (userScore == null) {
            return null;
        }
        return UserScoreVO.builder()
                .id(userScore.getId())
                .userId(userScore.getUserId())
                .score(userScore.getScore())
                .description(userScore.getDescription())
                .rankName(RankEnum.getByCode(userScore.getRankName()).getName())
                .totalMatches(userScore.getTotalMatches())
                .winMatches(userScore.getWinMatches())
                .loseMatches(userScore.getLoseMatches())
                .build();
    }

    public static UserScoreVO toUserScoreVO(UserScoreDTO userScoreDTO) {
        if (userScoreDTO == null) {
            return null;
        }
        return UserScoreVO.builder()
                .id(userScoreDTO.getId())
                .userId(userScoreDTO.getUserId())
                .score(userScoreDTO.getScore())
                .description(userScoreDTO.getDescription())
                .rankName(RankEnum.getByCode(userScoreDTO.getRankName()).getName())
                .totalMatches(userScoreDTO.getTotalMatches())
                .winMatches(userScoreDTO.getWinMatches())
                .loseMatches(userScoreDTO.getLoseMatches())
                .username(userScoreDTO.getUsername())
                .avatar(userScoreDTO.getAvatar())
                .build();
    }

    /**
     * 将当前 UserScoreVO 转换为 UserScore 实体
     */
    public UserScore toUserScore() {
        return UserScore.builder()
                .id(this.getId())
                .userId(this.getUserId())
                .score(this.getScore())
                .description(this.getDescription())
                .rankName(this.getRankName())
                .totalMatches(this.getTotalMatches())
                .winMatches(this.getWinMatches())
                .loseMatches(this.getLoseMatches())
                .build();
    }
}